library(tidyverse)
library(haven)
library(labelled)
library(scales)
library(purrr)
library(tidyr)
library(broom)
rm(list=ls())


df<- read_rds("clean-GSS-institutional-trust.RDS")%>%
  subset(is.na(mode) | mode==1)


## make it longer
df_long <- df %>%
  pivot_longer(consci:conlegis,names_to = "institution", values_to = "value")%>%
  #drop_na() %>%
  mutate(institution = as.character(recode(institution, 
                                           "consci" = "Scientific Community",
                                           "coneduc" = "Education",
                                           "conpress" = "Press",
                                           "contv"="Television",
                                           "conjudge" = "U.S. Supreme Court",
                                           "conmedic" = "Medicine",
                                           "conarmy" = "Military",
                                           "conclerg" = "Organized Religion",
                                           "conbus" = "Major Companies",
                                           "conlabor" = "Organized Labor",
                                           "confinan" = "Banks and Financial Institutions",
                                           "conlegis" = "Congress"))) 

means <- df_long %>%
  mutate(value = as.numeric(recode(value, 
                                   "1" = "3",
                                   "2" = "2",
                                   "3" = "1"))) %>%
  group_by(year, institution) %>%
  summarise(mean_trust_i_per_year = weighted.mean(value, wtssps, na.rm=TRUE),
            sd = sd(value, na.rm=TRUE),
            se = sd / sqrt(n()),
            n_non_na = sum(!is.na(value)))

#get total n
sum(means$n_non_na)

ggplot(means, aes(x=year, y=mean_trust_i_per_year)) + geom_point() +
  geom_line(linetype="dashed") +
  theme_bw() +
  facet_wrap(vars(institution)) +
  geom_errorbar(aes(ymin = (mean_trust_i_per_year -1.96*se), 
                    ymax = (mean_trust_i_per_year + 1.96*se))) +
  labs(x="", y="Category",
       subtitle="As far as the people running these institutions are concerned, would you 
say you have a great deal of confidence, only some confidence, or hardly
any confidence at all in them? N=513,603 (GSS)
Mean and 95% Confidence Intervals") +
  scale_y_continuous(limits = c(1,3), breaks=c(1,2,3),
                     labels = c("Hardly Any", "Only Some", "A Great Deal"))
ggsave("PNAS/3-FTF.png", width=12, height=8)